package org.dromara.iot.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.iot.domain.bo.IotUpkeepTemplateBo;
import org.dromara.iot.domain.vo.IotUpkeepTemplateVo;
import org.dromara.iot.service.IIotUpkeepTemplateService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 电梯保养模板
 *
 * @author lsl
 * @date 2024-06-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/iot/upkeepTemplate")
public class IotUpkeepTemplateController extends BaseController {

    private final IIotUpkeepTemplateService iotUpkeepTemplateService;

    /**
     * 查询电梯保养模板列表
     */
    @SaCheckPermission("iot:upkeepTemplate:list")
    @GetMapping("/list")
    public TableDataInfo<IotUpkeepTemplateVo> list(IotUpkeepTemplateBo bo, PageQuery pageQuery) {
        return iotUpkeepTemplateService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出电梯保养模板列表
     */
    @SaCheckPermission("iot:upkeepTemplate:export")
    @Log(title = "电梯保养模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(IotUpkeepTemplateBo bo, HttpServletResponse response) {
        List<IotUpkeepTemplateVo> list = iotUpkeepTemplateService.queryList(bo);
        ExcelUtil.exportExcel(list, "电梯保养模板", IotUpkeepTemplateVo.class, response);
    }

    /**
     * 获取电梯保养模板详细信息
     *
     * @param templateId 主键
     */
    @SaCheckPermission("iot:upkeepTemplate:query")
    @GetMapping("/{templateId}")
    public R<IotUpkeepTemplateVo> getInfo(@NotNull(message = "主键不能为空")
                                          @PathVariable Long templateId) {
        return R.ok(iotUpkeepTemplateService.queryById(templateId));
    }

    /**
     * 新增电梯保养模板
     */
    @SaCheckPermission("iot:upkeepTemplate:add")
    @Log(title = "电梯保养模板", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody IotUpkeepTemplateBo bo) {
        return toAjax(iotUpkeepTemplateService.insertByBo(bo));
    }

    /**
     * 修改电梯保养模板
     */
    @SaCheckPermission("iot:upkeepTemplate:edit")
    @Log(title = "电梯保养模板", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody IotUpkeepTemplateBo bo) {
        return toAjax(iotUpkeepTemplateService.updateByBo(bo));
    }

    /**
     * 删除电梯保养模板
     *
     * @param templateIds 主键串
     */
    @SaCheckPermission("iot:upkeepTemplate:remove")
    @Log(title = "电梯保养模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/{templateIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] templateIds) {
        return toAjax(iotUpkeepTemplateService.deleteWithValidByIds(List.of(templateIds), true));
    }
}
